package com.appwx.sdk.model;

import javax.crypto.spec.SecretKeySpec;

import org.json.JSONException;
import org.json.JSONObject;

public class Token {

	// mToken 可能是access token， 可能是oauth_token
	private String mToken = "";
	private String mRefreshToken = "";
	private long mExpiresIn = 0;
	private String openId = "";
	private String mOauth_verifier = "";
	private int keySrc;
	protected String mOauth_Token_Secret = "";
	protected String[] responseStr = null;
	protected SecretKeySpec mSecretKeySpec;

	public Token() {

	}

	public String getToken() {
		return this.mToken;
	}

	public String getRefreshToken() {
		return mRefreshToken;
	}

	public void setRefreshToken(String mRefreshToken) {
		this.mRefreshToken = mRefreshToken;
	}

	public long getExpiresIn() {
		return mExpiresIn;
	}

	public void setExpiresIn(long mExpiresIn) {
		this.mExpiresIn = mExpiresIn;
	}

	public void setExpiresIn(String expiresIn) {
		if (expiresIn != null && !expiresIn.equals("0")) {
			setExpiresIn(System.currentTimeMillis()/1000 + Long.parseLong(expiresIn));
		}
	}

	
	public void setToken(String mToken) {
		this.mToken = mToken;
	}

	public void setVerifier(String verifier) {
		mOauth_verifier = verifier;
	}

	public String getVerifier() {
		return mOauth_verifier;
	}

	public String getSecret() {
		return mOauth_Token_Secret;
	}

	public Token(String accessTokenResult, int source) throws JSONException {
		keySrc = source;
		JSONObject jsonObject = new JSONObject(accessTokenResult);
		if (!jsonObject.isNull("access_token")) {
			this.mToken = jsonObject.getString("access_token");
		}
		if (!jsonObject.isNull("expires_in")) {
			setExpiresIn(jsonObject.getString("expires_in"));
		}
		if (!jsonObject.isNull("uid")) {
			this.openId = jsonObject.getString("uid");
		}
	}

	public Token(String rltString) {
		responseStr = rltString.split("&");
		mOauth_Token_Secret = getParameter("oauth_token_secret");
		mToken = getParameter("oauth_token");
	}

	public Token(String token, String secret) {
		mToken = token;
		mOauth_Token_Secret = secret;
	}

	public String getParameter(String parameter) {
		String value = null;
		for (String str : responseStr) {
			if (str.startsWith(parameter + '=')) {
				value = str.split("=")[1].trim();
				break;
			}
		}
		return value;
	}

	public void setSecretKeySpec(SecretKeySpec secretKeySpec) {
		this.mSecretKeySpec = secretKeySpec;
	}

	public SecretKeySpec getSecretKeySpec() {
		return mSecretKeySpec;
	}

	public String getOpenId() {
		return openId;
	}

	public void setOpenId(String openId) {
		this.openId = openId;
	}

	@Override
	public String toString() {
		String s = "mToken = " + mToken + " mRefreshToken = " + mRefreshToken + " openId = " + openId
				+ " mExpiresIn = " + mExpiresIn;
		return s;
	}

	public int getKeySrc() {
		return keySrc;
	}

	public void setKeySrc(int keySrc) {
		this.keySrc = keySrc;
	}
}
